package net.lzzy.practices.web.course;

import net.lzzy.practices.constants.ApiConstant;
import net.lzzy.practices.constants.Messages;
import net.lzzy.practices.entities.Applicant;
import net.lzzy.practices.entities.Course;
import net.lzzy.practices.services.ApplicantService;
import net.lzzy.practices.utils.StringUtils;
import net.lzzy.practices.web.models.ApiResult;
import net.lzzy.practices.web.models.ApplicantInfo;
import net.lzzy.practices.web.models.VmApplicant;
import net.lzzy.practices.web.models.VmCourse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

/**
 * @author hzl on 2022/5/26.
 * Description:
 */
@CrossOrigin
@RestController
@RequestMapping(ApiConstant.ROUTE_APPLICANT_ROOT)
public class ApplicantApiController {
    @Autowired
    public ApplicantApiController(ApplicantService service) {
        this.service = service;
    }

    private final ApplicantService service;


    @PostMapping(ApiConstant.ROUTE_APPLICANT_APPLY)
    public ApiResult apply(@RequestBody ApplicantInfo info){
        String token=info.getToken();
        String strCourseId=info.getCourseId();
        String strStudentId=info.getStudentId();
        if (StringUtils.isEmpty(token)||
        StringUtils.isEmpty(strCourseId)||
        StringUtils.isEmpty(strStudentId)){
            return new ApiResult(false, Messages.INCOMPLETE_INFO.toString(), null);

        }
        UUID studentId=UUID.fromString(strStudentId);
        UUID courseId=UUID.fromString(strCourseId);
        Applicant applicant=service.apply(studentId,courseId,token);
        if (applicant==null){
            String msg=Messages.NO_PERMISSION+"或"+Messages.WRONG_ID;
            return new ApiResult(false,msg,null);

        }
        return new ApiResult(true,Messages.SUCCESS.toString(),new VmApplicant(applicant));
    }

    @GetMapping(ApiConstant.ROUTE_APPLICANT_ENROLLED)
    public ApiResult getEnrolledCourses(@RequestParam String token,@RequestParam(name = "id")UUID studentId){
        List<Applicant> applicants=service.getEnrolledApplicants(studentId);
        return transform(applicants);
    }
    @GetMapping(ApiConstant.ROUTE_APPLICANT_APPLYING)
    public ApiResult getApplyingCourses(@RequestParam String token,@RequestParam(name = "id")UUID studentId){
        List<Applicant> applicants=service.getApplyingApplicants(studentId);
        return transform(applicants);
    }
    @GetMapping(ApiConstant.ROUTE_APPLICANT_DECLINED)
    public ApiResult getDeclinedCourses(@RequestParam String token,@RequestParam(name = "id")UUID studentId){
        List<Applicant> applicants=service.getDeclinedApplicants(studentId);
        return transform(applicants);
    }
    private ApiResult transform(List<Applicant> applicants){
        if (applicants==null){
            return new ApiResult(false,Messages.WRONG_ID.toString(), null);
        }
        List<VmApplicant>vmApplicants=new ArrayList<>();
        applicants.forEach(applicant -> vmApplicants.add(new VmApplicant(applicant)));
        return new ApiResult(true,applicants.size()+"",vmApplicants);
    }
    @PostMapping(ApiConstant.ROUTE_APPLICANT_APPROVE)
    public ApiResult approve(@RequestParam String token,@RequestParam(name ="id" )UUID applicantId){
        Applicant applicant =service.approve(applicantId,token);
        if (applicant==null){
            String msg=Messages.NO_PERMISSION+"或"+Messages.WRONG_ID;
            return new ApiResult(false,msg,null);
        }
        return new ApiResult(true,Messages.SUCCESS.toString(),new VmApplicant(applicant));
    }

    @PostMapping(ApiConstant.ROUTE_APPLICANT_DECLINE)
    public ApiResult decline(@RequestParam String token,@RequestParam(name ="id" )UUID applicantId){
        Applicant applicant =service.decline(applicantId,token);
        if (applicant==null){
            String msg=Messages.NO_PERMISSION+"或"+Messages.WRONG_ID;
            return new ApiResult(false,msg,null);
        }
        return new ApiResult(true,Messages.SUCCESS.toString(),new VmApplicant(applicant));
    }
    @GetMapping(ApiConstant.ROUTE_APPLICANT_TEACHER_ENROLLED)
    public ApiResult getEnrolledStudents(@RequestParam String token,@RequestParam(name = "id")UUID courseId){
        List<Applicant> applicants=service.getEnrolledStudents(courseId);
        return transform(applicants);
    }
    @GetMapping(ApiConstant.ROUTE_APPLICANT_TEACHER_APPLYING)
    public ApiResult getApplyingStudents(@RequestParam String token,@RequestParam(name = "id")UUID courseId){
        List<Applicant> applicants=service.getApplyingStudents(courseId);
        return transform(applicants);
    }
    @GetMapping(ApiConstant.ROUTE_APPLICANT_TEACHER_DECLINED)
    public ApiResult getDeclinedStudents(@RequestParam String token,@RequestParam(name = "id")UUID courseId){
        List<Applicant> applicants=service.getDeclineStudents(courseId);
        return transform(applicants);
    }

    @PostMapping(ApiConstant.ROUTE_APPLICANT_TEACHER_REMOVE)
    public ApiResult remove(@RequestBody ApplicantInfo info){
        String token =info.getToken();
        String strCourseId=info.getCourseId();
        String strStudentId=info.getStudentId();
        if (StringUtils.isEmpty(token)||
                StringUtils.isEmpty(strCourseId)||
                StringUtils.isEmpty(strStudentId)){
            return new ApiResult(false, Messages.INCOMPLETE_INFO.toString(), null);
        }
        UUID studentId=UUID.fromString(strStudentId);
        UUID courseId=UUID.fromString(strCourseId);
        Course course=service.removeStudent(courseId,studentId,token);
        if (course==null){
            String msg=Messages.NO_PERMISSION+"或"+Messages.WRONG_ID;
            return new ApiResult(false,msg,null);

        }
        return new ApiResult(true,Messages.SUCCESS.toString(), VmCourse.create(course,true));
    }
}
